import axios from 'axios'
import { Message } from "@arco-design/web-vue";

// 是否是开发环境
export const isDev = process.env.NODE_ENV === "development";

// 创建 Axios 实例
const myAxios = axios.create({
  baseURL: isDev ? "http://localhost:8101" : "https://aiback2-124047-7-1325096849.sh.run.tcloudbase.com/",
  timeout: 60000,
  withCredentials: true,
});

// const myAxios = axios.create({
//   baseURL: 'http://localhost:8101',
//   timeout: 60000,
//   withCredentials: true // 允许跨域请求时携带cookie
// })

myAxios.interceptors.request.use(config => {
  return config
})


myAxios.interceptors.response.use(response => {
  // console.log(response)

  const { data } = response
  if (data.code === 40100) {
    // 不是获取用户信息的请求,或者用户不是在登录页,跳转到登录页
    if(!response.request.responseURL.includes('user/get/login') && !window.location.pathname.includes('/user/login')) {
      Message.warning('请先登录')
      window.location.href = `/user/login?redirect=${window.location.href}`
    }
  }
  return response
})


export default myAxios